package net.lucamasini.fantacalcio.gwt.client.view;

import static net.lucamasini.fantacalcio.gwt.client.Fantacalcio.getEventBus;

import java.util.Date;

import net.lucamasini.fantacalcio.gwt.client.event.HomeEvent;
import net.lucamasini.fantacalcio.gwt.client.event.SalvaGiornataEvent;
import net.lucamasini.fantacalcio.server.model.Partita;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.view.client.HasData;

public class GiornataPanel extends Composite implements GiornataView {
    interface Binder extends UiBinder<Widget, GiornataPanel> {
    }

    private static final Binder BINDER = GWT.create(Binder.class);

    public GiornataPanel() {
        initWidget(BINDER.createAndBindUi(this));
        
        final Column<Partita, String> casa = new Column<Partita, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Partita, String>() {

        			@Override
        			public void update(int index, Partita object, String value) {

    					object.setCasa(value);	
        			}
        		});
        	}
			@Override
			public String getValue(Partita object) {
				return object.getCasa();
			}
		};
		
        final Column<Partita, String> trasferta = new Column<Partita, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<Partita, String>() {

        			@Override
        			public void update(int index, Partita object, String value) {

        				object.setTrasferta(value);
        			}
        		});
        	}
			@Override
			public String getValue(Partita object) {
				return object.getTrasferta();
			}
		};

		partiteCellTable.addColumn(casa, "CASA");
        partiteCellTable.addColumn(trasferta, "TRASFERTA");
    }

    @UiField TextBox descrizione;
    @UiField(provided = true) DateBox dataInizioPronostici = new DateBox(new DatePicker(), null, new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
    @UiField(provided = true) DateBox dataFinePronostici = new DateBox(new DatePicker(), null, new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
    
    @UiField CellTable<Partita> partiteCellTable;
    @UiField Hyperlink home;
    
    @UiField Button salvaButton;

    @UiHandler("salvaButton")
    void confermaClicked(ClickEvent event) {
    	getEventBus().fireEvent(new SalvaGiornataEvent());
    }
    
    @UiHandler("home")
    void homeClicked(ClickEvent event) {
    	getEventBus().fireEvent(new HomeEvent());
    }
    
    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void setName(String name) {
        throw new UnsupportedOperationException("setName not supported yet in "+this.getClass().getName());
    }

    @Override
    public String getName() {
        return "Nuova Giornata";
    }
    
	@Override
	public HasData<Partita> partite() {
		return partiteCellTable;
	}

	@Override
	public HasValue<String> descrizione() {
		return descrizione;
	}

	@Override
	public HasValue<Date> dataInizioPronostici() {
		return dataInizioPronostici;
	}

	@Override
	public HasValue<Date> dataFinePronostici() {
		return dataFinePronostici;
	}
}
